[% setvar title my Dog $spot should call a constructor implicitly %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>my Dog $spot should call a constructor implicitly</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Michael Fowler &lt;<a href='mailto:michael@shoebox.net'>michael@shoebox.net</a>&gt;
  Date: 29 August 2000
  Mailing List: <a href='mailto:perl6-language-objects@perl.org'>perl6-language-objects@perl.org</a>
  Number: 171
  Version: 3
  Status: Retracted</pre>
<a name='NOTES ON RETRACTION'></a><h1>NOTES ON RETRACTION</h1>
<p>The consensus was that my Dog $spot should simply be an assertion that $spot
isa Dog, and nothing more (see RFC 218).  In fact, nobody truly agreed with
this RFC, or me.</p>
<p>That is not to say I still don't like the idea.  I still think it's an
intuitive and simple approach to object creation, moreso than any
alternatives offered.</p>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>The current optimization behaviour of the syntax my Dog $spot should be
replaced with a call to some implicit constructor, which creates a Dog
object out of the $spot variable.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>What is currently an optimization for pseudo-hashes:</p>
<pre>    my Dog $spot = Dog-&gt;new();</pre>
<p>should be replaced with:</p>
<pre>    my Dog $spot;</pre>
<p>which calls an implicit constructor (discussed further in the IMPLEMENTATION
section).  The optimization behaviour can be retained in some form (see the
MIGRATION section).</p>
<p>This syntax can also be extended to provide a more robust constructor,
allowing the specification of arguments:</p>
<pre>    my Dog $spot (color =&gt; &quot;brown&quot;, name =&gt; &quot;Spot&quot;, answers_to =&gt; &quot;yo dog!&quot;);</pre>
<a name='Examples'></a><h2>Examples</h2>
<p>This is most readable and useful when declaring types, as in:</p>
<pre>    my int $i = 4;
    my float $f;</pre>
<p>And could be extended into lists, arrays, and hashes:</p>
<pre>    my int ($x, $y, $z);
    my float ($price, $tax) = (4.90, 0.04);
    my array_ref @matrix;       # list of lists
    my int %counters;           # hash of integer values, possibly optimized</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>my Dog $spot resolves to a method call in the Dog package.  This method is
called as if it were called as a class method with an optional argument (in
the case of assignment).  For example, the syntax:</p>
<pre>    my Dog $spot = &quot;Spot&quot;;</pre>
<p>would be transformed to, or be the equivalent of:</p>
<pre>    $spot = Dog-&gt;$METHOD(&quot;Spot&quot;);</pre>
<p>If the multi-argument form of the constructor were to be adopted this would
have to change.  Dog-&gt;$METHOD would either have to return a tied scalar,
thus being the equivalent of:</p>
<pre>    $spot = Dog-&gt;$METHOD();
    tied($spot)-&gt;STORE(&quot;Spot&quot;);</pre>
<p>Or RFC159 would have to be adopted, producing the equivalent:</p>
<pre>    $spot = Dog-&gt;$METHOD();
    $spot-&gt;STORE(&quot;Spot&quot;);</pre>
<p>And, finally, the syntax:</p>
<pre>    my Dog $spot (@args);</pre>
<p>would be the equivalent of:</p>
<pre>    $spot = Dog-&gt;$METHOD(@args);</pre>
<a name='Constructor'></a><h2>Constructor</h2>
<p>Listed below are constructor alternatives.  If a sub is chosen (as in sub
PREPARE {}) it will suffer from the disadvantage of potentially causing
problems with current Perl modules.</p>
<p>If an appropriate constructor is not provided a fatal exception should be
raised when the syntax is used.</p>
<ul>
<li><a name='sub PREPARE {}'></a>sub PREPARE {}</li>
<p>This was the name Ilya Zakharevich and p5p decided on in a similar proposal
for Perl 5 (see the REFERENCES section).</p>
<li><a name='sub CREATE {}'></a>sub CREATE {}</li>
<p>As RFC 159 somewhat implies the syntax could resolve to the CREATE method.</p>
<li><a name='sub NEW {}'></a>sub NEW {}</li>
<p>Like CREATE, but maps a little more cleanly to the large body of modules.</p>
<li><a name='author-defined'></a>author-defined</li>
<p>Allow the author to define an implicit constructor.  This would be the most
flexible method, needing only one constructor to be defined (unless
semantics differ, of course).</p>
<p>This definition would probably be best provided through a pragma:</p>
<pre>    use constructor qw(new);</pre>
<p>perhaps even dropped on top of overload:</p>
<pre>    use overload constructor =&gt; 'new';</pre>
</ul>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>All current my Dog $spot constructs will have to be converted to simply my
$spot.</p>
<p>The optimization provided by the current syntax could be retained, in a
slightly different form.  If an object is constructed using my Dog $spot
compile- and run-time checks could be put in place to force the $spot object
to <b>always</b> be blessed into the &quot;Dog&quot; package, raising a fatal exception if
this promise is broken in any way.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<pre>    version 3
        - changed status to &quot;Retracted&quot;, added a NOTES ON RETRACTION section
        - removed the $METHOD {} suggestions
        - fixed the tied scalar example

    version 2
        - made it a little clearer that this is to replace the current
          meaning of the syntax
        - replaced the MIGRATION section with better migration method
        - added a PREPARE constructor alternative
        - made a distinction between sub $METHOD {} and $METHOD {} in the
          constructor alternatives list
        - added a suggestion for the list argument form of the constructor,
          with some alternatives to the my Dog $spot if it's adopted
        - added some examples of what the syntax could be useful for
        - removed lvalue RFC references that shouldn't have made it into v1,
          added some p5p archive references
        - added this CHANGES section</pre>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 159: True Polymorphic Objects</p>
<p>p5p archives -- each link is the start or continuation of the discussion</p>
<p>my Number $n
<a href='http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-04/msg00955.html' target='_blank'>www.xray.mpe.mpg.de</a>
<a href='http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-08/msg00479.html' target='_blank'>www.xray.mpe.mpg.de</a>
<a href='http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-10/msg00356.html' target='_blank'>www.xray.mpe.mpg.de</a></p>
<p>[PATCH 5.005_62] my Dog $spot and prepare
<a href='http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-11/msg00882.html' target='_blank'>www.xray.mpe.mpg.de</a>
<a href='http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-12/msg00070.html' target='_blank'>www.xray.mpe.mpg.de</a></p>
<p>RFC 218: <code>my Dog $spot</code> is just an assertion</p>
</div>
